//  Copyright 2018 Istio Authors
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

package util

import (
	"fmt"
	"time"
)

const (
	// DefaultRetryTimeout is the default value for a total timeout duration for a retry operation.
	DefaultRetryTimeout = time.Second * 20

	// DefaultRetryWait is the default duration to wait between retries of an operation.
	DefaultRetryWait = time.Millisecond * 10
)

// Retry the given function, until there is a timeout, or until the function indicates that it has completed.
func Retry(
	timeout time.Duration,
	retryWait time.Duration,
	fn func() (result interface{}, completed bool, err error)) (interface{}, error) {

	var lasterr error
	to := time.After(timeout)
	for {
		select {
		case <-to:
			return nil, fmt.Errorf("timeout while waiting (last error: %v)", lasterr)
		default:
		}

		result, completed, err := fn()
		if completed {
			return result, err
		}
		if err != nil {
			lasterr = err
		}

		<-time.After(retryWait)
	}
}
